package casa.depesos.app.com.frag;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import casa.depesos.app.com.BuildConfig;
import casa.depesos.app.com.R;
import casa.depesos.app.com.activity.MainActivity;
import casa.depesos.app.com.activity.WebActivity;
import casa.depesos.app.com.databinding.FragmentCheckingBinding;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.HomeConfigBean;
import casa.depesos.app.com.http.bean.ScoreBean;
import casa.depesos.app.com.http.data.HomeConfig;
import casa.depesos.app.com.http.req.HomeConfigReq;
import casa.depesos.app.com.http.req.IsScoreReq;
import casa.depesos.app.com.it.Refreshable;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;

public class CheckingFragment extends BannerBaseFragment implements Refreshable {

    FragmentCheckingBinding binding;
    boolean isFirst;

    public CheckingFragment setStatus(boolean first) {
        isFirst = first;
        return this;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Report.show(isFirst ? "ACTION_QQ_001" : "ACTION_QQ_003");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentCheckingBinding.inflate(inflater, container, false);
        binding.rela.setPadding(0, Utils.getStatusBarHeight(getActivity()), 0, 0);
        if (isFirst) {
            binding.tip.setText(R.string.checking_tip_first);
        } else {
            binding.tip.setText(R.string.checking_tip);
        }

        if (Constant.white) {
            h.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Utils.setStatus(62);
                    ((MainActivity) getActivity()).getData();
                }
            }, BuildConfig.DEBUG ? 5000 : 90 * 1000);
        }
        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        update();
    }

    private void update() {
        getName(binding.top);
        getUnRead(binding.top);
        getLink(binding.bottom);
        getConfig();
        showMsg(binding.msg, binding.scroll);
        getBanner(binding.img);
        score();
    }

    private void getConfig() {
        new HomeConfigReq().req(new ReqCallback<HomeConfigBean>() {
            @Override
            public void callback(boolean success, HomeConfigBean bean) {
                if (Constant.white) {
                    bean = new HomeConfigBean();
                    bean.config = new HomeConfig();
                    bean.config.amount = "10000";
                }
                if (bean != null && bean.isValid()) {
                    binding.s.setVisibility(View.VISIBLE);
                    binding.max.setText(Utils.formatNoFloat(bean.config.amount));
                }
            }
        });
    }

    private void score() {
        new IsScoreReq().req(new ReqCallback<ScoreBean>() {
            @Override
            public void callback(boolean success, ScoreBean bean) {
                if (success && bean.isValid()) {
                    if ("1".equalsIgnoreCase(bean.info.open)) {
                        binding.take.setVisibility(View.VISIBLE);
                        binding.take.setOnClickListener(v -> {
                            Report.click(isFirst ? "ACTION_QQ_002" : "ACTION_QQ_004");
                            WebActivity.startGooglePlay(bean.info.link);
                        });
                    } else {
                        binding.take.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    @Override
    public void refresh() {
        update();
    }
}
